package com.young.aspect;

import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: 全局异常处理
 * @Author: kangyang
 * @Date: 2021/5/20 10:07
 **/
@RestControllerAdvice
public class WebErrorLog {

  private static final Logger log = LoggerFactory.getLogger(WebErrorLog.class);

  // 精确->模糊
  @ExceptionHandler(RuntimeException.class)
  public ResponseEntity<String> runtimeLog(RuntimeException e) {
    // 1. 对开发人员怎么看
    log.error("运行时异常", e.getMessage());
    // 2. 对用户怎么看
    return ResponseEntity.badRequest().body(e.getMessage());
  }

  @ExceptionHandler(UnauthorizedException.class)
  public ResponseEntity<Void> authPermission(UnauthorizedException e) {
    log.error("没有权限", e.getMessage());
    return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
  }
}
